/*
 * File Name：BottomMenuDialog.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： BottomMenuDialog.java
 * Modify By：res-jianpingliu
 * Modify Date：2014-9-18
 * Modify Type：Add
 */
package com.ciwong.mobilelib.widget;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.ciwong.mobilelib.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 从底部弹出的带多个按钮的选择菜单
 *
 * @author res-jianpingliu
 * @version ciwong v.1.0 2014-9-18
 * @since ciwong v.1.0
 */
public class BottomMenuDialog extends CWDialog {

    private final int mDefaultMargin = 10;

    private final int mDefaultHeight = 60;

    private final int two = 2;

    private final float mDefaultTextSize = 17f;

    private final int mDefaultTextColor = 0xFF3381e3;

    /**
     * 其它按钮颜色
     */
    private int otherBtnColor = mDefaultTextColor;

    /**
     * 最后按钮颜色
     */
    private int lastBtnColor = mDefaultTextColor;

    private LinearLayout mChooseMenu;

    private ArrayList<Button> mBtnList;

    private ArrayList<View.OnClickListener> mListenerList, mTempListenerList;

    private ArrayList<String> mBtnTextList, mTempTextList;

    private int selectorIndex;

    private boolean isSelector = false;

    List<SpannableStringBuilder> mBtnTextList2,mTempTextList2;

    public BottomMenuDialog(Context context) {
        super(context);

        init();
    }

    /**
     * 设置字体颜色
     *
     * @param color
     */
    public void setBtnTextColor(int color) {

        this.otherBtnColor = color;
    }

    /**
     * 设置最后字体颜色
     *
     * @param color
     */
    public void setLastBtnTextColor(int color) {

        this.lastBtnColor = color;
    }

    /**
     * dip 转换成px
     *
     * @param dip dp值
     * @return px值
     */
    private int formatDimension(int dip) {
        DisplayMetrics displayMetrics = mContext.getResources()
                .getDisplayMetrics();

        TypedValue typedValue = new TypedValue();
        int size = (int) typedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dip, displayMetrics);
        return size;
    }

    /**
     * sp 转换成px
     *
     * @param sp sp值
     * @return px值
     */
    private int formatTextSize(int sp) {
        DisplayMetrics displayMetrics = mContext.getResources()
                .getDisplayMetrics();

        TypedValue typedValue = new TypedValue();
        int size = (int) typedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                sp, displayMetrics);
        return size;
    }

    private void init() {
        mChooseMenu = new LinearLayout(mContext);
        mChooseMenu.setOrientation(LinearLayout.VERTICAL);

        setContentView(mChooseMenu, true);
        setCanceledOnTouchOutside(true);
        mBtnList = new ArrayList<Button>();
        mBtnTextList = new ArrayList<String>();
        mListenerList = new ArrayList<View.OnClickListener>();
        mTempTextList = mBtnTextList;
        mTempTextList2 = mBtnTextList2;
        mTempListenerList = mListenerList;
    }

    /**
     * 显示菜单
     */
    public void show() {
        super.showFromBottom();
    }

    private Button createButton(String text, int drawable) {
        Button btn = new Button(mContext);
        setButtonStyle(btn);
        btn.setBackgroundResource(drawable);
        btn.setText(text);
        return btn;
    }

    private Button createPublishButton(SpannableStringBuilder text, int drawable) {
        Button btn = new Button(mContext);
        setButtonStyle(btn);
        btn.setBackgroundResource(drawable);
        btn.setText(text);
        return btn;
    }

    /**
     * 添加从上到下第一个按钮 上圆角下方角
     *
     * @param text 按钮文本
     */
    private Button creatFirstButton(String text) {
        return createButton(text, R.drawable.change_face_up_btn_sel);
    }

    private Button creatPublishFirstButton(SpannableStringBuilder text) {
        return createPublishButton(text, R.drawable.change_face_up_btn_sel);
    }

    /**
     * 添加中间的按钮 方角
     *
     * @param text 按钮文本
     */
    private Button createMiddleButton(String text) {
        return createButton(text, R.drawable.change_face_middle_btn_sel);
        // return createButton(text, R.drawable.change_face_up_btn_sel);
    }

    /**
     * 添加中间的按钮 方角
     *
     * @param text 按钮文本
     */
    private Button createPublishMiddleButton(SpannableStringBuilder text) {
        return createPublishButton(text, R.drawable.change_face_middle_btn_sel);
        // return createButton(text, R.drawable.change_face_up_btn_sel);
    }

    /**
     * 添加从上到下倒数最二个按钮 上方角下圆角
     *
     * @param text 按钮文本
     */
    private Button createLastSecondButton(String text, boolean isOnlyTwoButton) {
        if (isOnlyTwoButton) {
            // 如果只有两个button那么第一个button也是用四个圆角
            return createButton(text, R.drawable.change_face_btn_sel);
            // return createButton(text, R.drawable.change_face_up_btn_sel);
        }
        return createButton(text, R.drawable.change_face_down_btn_sel);
        // return createButton(text, R.drawable.change_face_up_btn_sel);
    }

    /**
     * 添加从上到下倒数最二个按钮 上方角下圆角
     *
     * @param text 按钮文本
     */
    private Button createPublishLastSecondButton(SpannableStringBuilder text, boolean isOnlyTwoButton) {
        if (isOnlyTwoButton) {
            // 如果只有两个button那么第一个button也是用四个圆角
            return createPublishButton(text, R.drawable.change_face_btn_sel);
            // return createButton(text, R.drawable.change_face_up_btn_sel);
        }
        return createPublishButton(text, R.drawable.change_face_down_btn_sel);
        // return createButton(text, R.drawable.change_face_up_btn_sel);
    }

    /**
     * 设置最后一个按钮的文本 圆角
     *
     * @param text 按钮文本
     */
    private Button createLastButton(String text) {
        Button lastBtn = createButton(text, R.drawable.change_face_btn_sel);
        // Button lastBtn = createButton(text,
        // R.drawable.change_face_up_btn_sel);
        lastBtn.setTextColor(lastBtnColor);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) lastBtn
                .getLayoutParams();

        layoutParams.topMargin = formatDimension(mDefaultMargin);
        layoutParams.bottomMargin = formatDimension(mDefaultMargin);

        lastBtn.setLayoutParams(layoutParams);

        return lastBtn;
    }
    /**
     * 设置最后一个按钮的文本 圆角
     *
     * @param text 按钮文本
     */
    private Button createPublishLastButton(SpannableStringBuilder text) {
        Button lastBtn = createPublishButton(text, R.drawable.change_face_btn_sel);
        // Button lastBtn = createButton(text,
        // R.drawable.change_face_up_btn_sel);
        lastBtn.setTextColor(lastBtnColor);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) lastBtn
                .getLayoutParams();

        layoutParams.topMargin = formatDimension(mDefaultMargin);
        layoutParams.bottomMargin = formatDimension(mDefaultMargin);

        lastBtn.setLayoutParams(layoutParams);

        return lastBtn;
    }


    private void setButtonStyle(Button button) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, formatDimension(mDefaultHeight));
        layoutParams.leftMargin = formatDimension(mDefaultMargin);
        layoutParams.rightMargin = formatDimension(mDefaultMargin);
        layoutParams.gravity = Gravity.CENTER;
        button.setLayoutParams(layoutParams);
        button.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultTextSize);
        button.setTextColor(otherBtnColor);
    }

    /**
     * 设置所有按钮的监听事件 必须是从上到下的顺序
     *
     * @param listeners 按钮的监听事件数组
     */
    public void setMenuOnClickListeners(
            ArrayList<View.OnClickListener> listeners) {
        mListenerList = listeners;
        setMenuOnClickListeners();
    }

    private void setMenuOnClickListeners() {
        if (mListenerList != null) {
            for (int i = 0; i < mListenerList.size(); i++) {
                mBtnList.get(i).setOnClickListener(mListenerList.get(i));
            }
        }

        mTempTextList = mBtnTextList;
        mTempListenerList = mListenerList;
    }

    public void setPublishMenuOnClickListeners(
            ArrayList<View.OnClickListener> listeners) {
        mListenerList = listeners;
        setPublishMenuOnClickListeners();
    }

    private void setPublishMenuOnClickListeners() {
        if (mListenerList != null) {
            for (int i = 0; i < mListenerList.size(); i++) {
                mBtnList.get(i).setOnClickListener(mListenerList.get(i));
            }
        }

        mTempTextList2 = mBtnTextList2;
        mTempListenerList = mListenerList;
    }

    /**
     * 设置所有按钮的字符
     *
     * @param list 字符数组
     */
    public void setMenuTextList(ArrayList<String> list) {
        mBtnTextList = list;
        setMenuText();
    }
    /**
     * 设置所有按钮的字符
     *
     * @param list 字符数组
     */
    public void setPublishMenuTextList(ArrayList<SpannableStringBuilder> list) {
        mBtnTextList2 = list;
        setPublishMenuText();
    }
    

    public void setSeletor(boolean isSelector) {

        this.isSelector = isSelector;
    }

    /**
     * 设置选择的项索引
     *
     * @param selectorIndex
     */
    public void setSeleted(int selectorIndex) {
        this.selectorIndex = selectorIndex;
    }

    private void setMenuText() {
        mBtnList = new ArrayList<Button>();
        mChooseMenu.removeAllViews();
        int size = mBtnTextList.size();

        for (int i = 0; i < size; i++) {
            String text = mBtnTextList.get(i);
            Button btn = null;
            if (i == 0 && size != two) {
                btn = creatFirstButton(text);
            } else if (i == 0 && size == two) {
                btn = createLastSecondButton(text, true);
            } else if (i != 0 && i == size - 1) {
                btn = createLastButton(text);
            } else if (i != 0 && i == size - two) {
                btn = createLastSecondButton(text, false);
            } else {
                btn = createMiddleButton(text);
            }

            if (isSelector) {
                if (selectorIndex == i) {
                    btn.setCompoundDrawablesWithIntrinsicBounds(
                            null,
                            null,
                            mContext.getResources().getDrawable(
                                    R.mipmap.libs_cw_checkbox_selected), null);
                    btn.setPadding(40, 0, 10, 0);
                } else {
                    btn.setPadding(40, 0, 40, 0);
                }
            }

            mBtnList.add(btn);
            mChooseMenu.addView(btn);
        }
    }


    private void setPublishMenuText() {
        mBtnList = new ArrayList<Button>();
        mChooseMenu.removeAllViews();
        int size = mBtnTextList2.size();

        for (int i = 0; i < size; i++) {
            SpannableStringBuilder text = mBtnTextList2.get(i);
            Button btn = null;
            if (i == 0 && size != two) {
                btn = creatPublishFirstButton(text);
            } else if (i == 0 && size == two) {
                btn = createPublishLastSecondButton(text, true);
            } else if (i != 0 && i == size - 1) {
                btn = createPublishLastButton(text);
            } else if (i != 0 && i == size - two) {
                btn = createPublishLastSecondButton(text, false);
            } else {
                btn = createPublishMiddleButton(text);
            }

            if (isSelector) {
                if (selectorIndex == i) {
                    btn.setCompoundDrawablesWithIntrinsicBounds(
                            null,
                            null,
                            mContext.getResources().getDrawable(
                                    R.mipmap.libs_cw_checkbox_selected), null);
                    btn.setPadding(40, 0, 10, 0);
                } else {
                    btn.setPadding(40, 0, 40, 0);
                }
            }

            mBtnList.add(btn);
            mChooseMenu.addView(btn);
        }
    }

    private void refereshMenu() {
        setMenuText();
        setMenuOnClickListeners();
    }

    /**
     * 隐藏一项
     *
     * @param index 索引
     */
    public void removeItem(int index) {
        if (index < 0 || index >= mTempTextList.size()
                || index >= mTempListenerList.size()) {
            return;
        }
        mTempTextList.remove(index);
        mTempListenerList.remove(index);
        mBtnTextList = mTempTextList;
        mListenerList = mTempListenerList;
        refereshMenu();
    }

    /**
     * 插入一项
     *
     * @param index    索引
     * @param text     插入项的字符
     * @param listener 插入项的监听
     */
    public void addItem(int index, String text, View.OnClickListener listener) {
        mTempTextList.add(index, text);
        mTempListenerList.add(index, listener);
        mBtnTextList = mTempTextList;
        mListenerList = mTempListenerList;
        refereshMenu();
    }

    /**
     * 设置一项的字符
     *
     * @param index    索引
     * @param text     字符
     * @param listener 事件监听
     */
    public void setItem(int index, String text, View.OnClickListener listener) {
        mBtnTextList.set(index, text);
        mListenerList.set(index, listener);
        refereshMenu();
    }

}
